/** session数据结构
 * @file session_data.hpp
 * @author Kiven Lee (kivensoft@gmail.com)
 * @version 0.1
 * @date 2022-07-09
 * 
 * @copyright Copyright (c) 2022
 * 
 */
#pragma once

#include "klib/utils.hpp"
#include "klib_http/session_manager.hpp"
#include "aidb.hpp"

namespace ai {

    struct ai_sess_data_t {
        using aidb_ptr_t = unique_ptr<aidb_t>;

        time_t          expire_time;
        time_t          update_time;
        string          user;
        string          pass;
        aidb_ptr_t      aidb_ptr;
        time_t          aidb_expire_time;

        void recycle(time_t now) {
            if (aidb_ptr && aidb_ptr->is_opened() && now > aidb_expire_time) {
                LOG_TRACE("recycle db[{}]", user);
                aidb_ptr->close();
            }
        }

        ai_sess_data_t() {}

        ai_sess_data_t(time_t expire_time, time_t update_time, string_view user, string_view pass,
                aidb_ptr_t&& aidb_ptr, time_t aidb_expire_time):
                    expire_time(expire_time), update_time(update_time), user(user), pass(pass),
                    aidb_ptr(std::move(aidb_ptr)), aidb_expire_time(aidb_expire_time) {}
    };

    using ai_session_manager_t = klib::http::session_manager_t<ai_sess_data_t>;

    
} // namespace ai
